package com.zjb.demo.common.controller;

import cn.hutool.core.date.DateUtil;
import com.alibaba.excel.EasyExcel;
import com.mybatisflex.core.paginate.Page;
import com.zjb.demo.common.domain.dto.SysLoginInfo;
import com.zjb.demo.common.domain.dto.SysOperationLog;
import com.zjb.demo.common.domain.response.AjaxResult;
import com.zjb.demo.system.domain.query.SysOperationLogQuery;
import com.zjb.demo.system.service.SysOperationLogService;
import io.swagger.annotations.Api;
import org.noear.solon.annotation.*;
import org.noear.solon.core.handle.Context;

import java.io.IOException;
import java.net.URLEncoder;
import java.nio.charset.StandardCharsets;
import java.util.Date;
import java.util.List;

/**
 * 操作日志
 * @author 庄佳彬
 * @since 2023/12/25 22:45
 */
@Api(tags = "操作日志")
@Controller
@Mapping("/monitor/operlog")
public class SysOperationLogController {

    @Inject
    private SysOperationLogService operationLogService;

    @Get
    @Mapping("/list")
    public AjaxResult<Page<SysOperationLog>> list(SysOperationLogQuery query){
        return AjaxResult.success(operationLogService.pageList(query));
    }

    @Get
    @Mapping("/export")
    public void export(SysOperationLogQuery query) throws IOException {
        var list = operationLogService.list(query);
        var ctx = Context.current();
        ctx.contentType("application/vnd.openxmlformats-officedocument.spreadsheetml.sheet");
        ctx.headerAdd("Content-Disposition", "attachment;filename=" + URLEncoder.encode("操作日志", StandardCharsets.UTF_8) + DateUtil.format(new Date(), "yyyyMMddHHmmss") + ".xlsx");
        var os = ctx.outputStream();
        EasyExcel.write(os, SysLoginInfo.class)
                .sheet("操作日志")
                .doWrite(list);
    }
    @Delete
    @Mapping("/{operationIds}")
    public AjaxResult<Boolean> remove(Long[] operationIds){
        return AjaxResult.success(operationLogService.removeByIds(List.of(operationIds)));
    }

    @Delete
    @Mapping("/clean")
    public AjaxResult<Void> clean(){
        operationLogService.clean();
        return AjaxResult.success();
    }
}
